import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response, URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/toPromise';

@Injectable()
export class ScoreInputService {
  private scoreInputUrl: string = '/water/certificate/save';

  constructor(private http: Http) {
  }

  saveScore(scoreInfos: ScoreInfo[]): Promise<any> {
    let headers = new Headers({'Content-Type': 'application/json'});
    let options = new RequestOptions({headers: headers});

    return this.http.post(
        this.scoreInputUrl,
        {'scores': scoreInfos},
        options
    ).toPromise()
    .then((resp: Response) => resp.json() || {})
    .catch(this.handleError);
  }

  private handleError(error: Response | any) {
    let errMsg: string = '保存失败';
    if (error instanceof Response) {
      if (error.status > 200) {
        errMsg = '保存失败';
      }
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    return Promise.reject(errMsg);
  }
}

export class ScoreInfo {
  ids: string;
  score: number;
}
